﻿<!--
/*
 * Auto Currency Converter
 * -------------------------------------------------------------------
 * Licensed under the GPLv3 license, see LICENSE in root for details.
 *
 * background.html
 *
 * Copyright (c) 2010 Thomas Klein
 */
-->
﻿<html>
<script type="text/javascript" src="common.js"></script>
<script>
	/*First Time configuration*/
	if(localStorage.length == 0)
	{
		fetchConversionRates();
		localStorage["currency"] = "eur";
	}
	
	/*
		Checks wether the price on Amazon should be converted
	*/
	function needToConvertAmazonPrice(url)
	{
		var currency = localStorage["currency"];
		
		if(url == "undefinded")
			return false;		
		
		if(url.indexOf('amazon.com') != -1)
		{
			if(currency != "usd")
				return true;
			else
				return false;
		}
		else if(url.indexOf('amazon.co.uk') != -1)
		{
			if(currency != "gbp")
				return true;
			else
				return false;
		}
		else if(url.indexOf('amazon.ca') != -1)
		{
			if(currency != "cad")
				return true;
			else
				return false;
		}
		else if(url.indexOf('amazon.de') != -1)
		{
			if(currency != "eur")
				return true;
			else
				return false;
		}
		else
			return false;
			
		return false;
	}
	
	
	function getSite(url)
	{
		if(url == "undefinded")
			return "undefined";

		if(url.indexOf('amazon.com') != -1)
			return "amazon.com";
		else if(url.indexOf('amazon.co.uk') != -1)
			return "amazon.co.uk";
		else if(url.indexOf('amazon.ca') != -1)
			return "amazon.ca";
		else if(url.indexOf('amazon.de') != -1)
			return "amazon.de";
		else
			return "undefined";
	}
	
	function prepareAmazonRequest(url)
	{
		var pref_currency = localStorage["currency"];
		
		if(url == "undefinded")
			return false;		
		
		if(url.indexOf('amazon.com') != -1)
		{
				toeurorate = localStorage["rate_usd"];
				if(localStorage["currency"] == "eur")
				{
					nativerate = localStorage["rate_eur"];
					symbol = "EUR";
				}
				if(localStorage["currency"] == "gbp")
				{
					nativerate = localStorage["rate_gbp"];
					symbol = "GPB";
				}
				if(localStorage["currency"] == "usd")
				{
					nativerate = localStorage["rate_usd"];
					symbol = "USD";
				}
				if(localStorage["currency"] == "cad")
				{
					nativerate = localStorage["rate_cad"];
					symbol = "CAD";
				}
				if(localStorage["currency"] == "jpy")
				{
					nativerate = localStorage["rate_jpy"];
					symbol = "JPY";
				}
		}
		else if(url.indexOf('amazon.co.uk') != -1)
		{

		}
		else if(url.indexOf('amazon.ca') != -1)
		{

		}
		else if(url.indexOf('amazon.de') != -1)
		{

		}
		else
			return false;
			
		return false;
	}
	
	var currentsite = 'undefined';
	var toeurorate;
	var nativerate;
	var symbol = "";
	
	function onTabUpdate(tabId, changeInfo, tab)
	{
		//fetchConversionRates();
		if(changeInfo.status == "loading")
		{
			currentsite = getSite(tab.url);
		}
		if(changeInfo.status == "complete")
		{
		
			if(currentsite != "undefined")
			{
				if(needToConvertAmazonPrice(currentsite))
				{
					chrome.tabs.executeScript(null,{file: "main.js"});
					prepareAmazonRequest(currentsite)
					chrome.tabs.getSelected(null, function(tab)
					{
						chrome.tabs.sendRequest(tab.id, {ratio: "13", site: "amazon", currency: symbol, erate: toeurorate, nrate: nativerate}, function(response) {});
					});
				}
			}
		}
	}
	//add event listener
	chrome.tabs.onUpdated.addListener(onTabUpdate);
</script>
</html>
